//
//  ManageViewController.m
//  SrnTeacher
//
//  Created by iDeneb on 5/25/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ManageViewController.h"
#import "SrnTeacherAppDelegate.h"
#import "SwitchViewController.h"
#import "user.h"
#import "QuartzCore/QuartzCore.h"


#define ROW_HEIGHT 60

@implementation ManageViewController

@synthesize myscrollView;
#pragma mark Table View Controller Methods;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor lightGrayColor];
	
	count.text = [NSString stringWithFormat:@"%hu",[[SrnTeacherAppDelegate App].listOfAllUsers count]];
	myscrollView.layer.cornerRadius = 8;
	myscrollView.layer.masksToBounds = YES;
}

-(void)updateResult
{
	count.text = [NSString stringWithFormat:@"%hu",[[SrnTeacherAppDelegate App].listOfAllUsers count]];
	[myscrollView reloadData];
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[SrnTeacherAppDelegate App].listOfAllUsers count];
}

- (IBAction)done:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
    //if ([delegate respondsToSelector:@selector(setupViewController:didFinishSetupWithInfo:)]) {
    //    [delegate setupViewController:self didFinishSetupWithInfo:setupInfo];
    //}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [self tableViewCellWithReuseIdentifier:CellIdentifier];
	}
	
	// configureCell:cell forIndexPath: sets the text and image for the cell -- the method is factored out as it's also called during minuted-based updates.
	[self configureCell:cell forIndexPath:indexPath];
    
    // Set up the cell...
	//User *cellValue = [[shouji138AppDelegate App].listOfItems objectAtIndex:indexPath.row];
	//cell.text = cellValue.username;
	
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Configuring table view cells

#define NAME_TAG 1
#define TIME_TAG 2
#define IMAGE_TAG 3


#define LEFT_COLUMN_OFFSET 10.0
#define LEFT_COLUMN_WIDTH 120.0

#define MIDDLE_COLUMN_OFFSET 140.0
#define MIDDLE_COLUMN_WIDTH 70.0

#define RIGHT_COLUMN_OFFSET 220.0

#define MAIN_FONT_SIZE 18.0
#define LABEL_HEIGHT 26.0

#define IMAGE_SIDE 30.0

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier {
	
	/*
	 Create an instance of UITableViewCell and add tagged subviews for the name, local time, and quarter image of the time zone.
	 */
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
	
	/*
	 Create labels for the text fields; set the highlight color so that when the cell is selected it changes appropriately.
	 */
	UILabel *label;
	CGRect rect;
	
	
	// Create a label for the time zone name.
	rect = CGRectMake(LEFT_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, LEFT_COLUMN_WIDTH, LABEL_HEIGHT);
	label = [[UILabel alloc] initWithFrame:rect];
	label.tag = NAME_TAG;
	label.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
	label.adjustsFontSizeToFitWidth = YES;
	[cell.contentView addSubview:label];
	label.highlightedTextColor = [UIColor whiteColor];
	[label release];
	
	// Create a label for the mute/unmute.
	
	 rect = CGRectMake(MIDDLE_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, IMAGE_SIDE, IMAGE_SIDE);
	 //label = [[UILabel alloc] initWithFrame:rect];
	 UIButton *muteButton = [[UIButton alloc] initWithFrame:rect];
	
	 muteButton.tag = TIME_TAG;
	
	 [cell.contentView addSubview:muteButton];
	
	 [muteButton release];
	 
	
	// Create an image view for the quarter image.
	rect = CGRectMake(RIGHT_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, IMAGE_SIDE, IMAGE_SIDE);
	
	UIButton *removeButton = [[UIButton alloc] initWithFrame:rect];
	removeButton.tag = IMAGE_TAG;
	[cell.contentView addSubview:removeButton];
	[removeButton release];	
	
	return cell;
}


- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    
	NSLog(@"cell index is %hu, tags imsgeTag is %hu",indexPath.row,IMAGE_TAG);
	User *cellValue = [[SrnTeacherAppDelegate App].listOfAllUsers objectAtIndex:indexPath.row];
	
	UILabel *label;
	
	// Set the locale name.
	label = (UILabel *)[cell viewWithTag:NAME_TAG];
	label.text = cellValue.username;
	
	// Set the time.
	/*
	 [dateFormatter setTimeZone:wrapper.timeZone];
	 label = (UILabel *)[cell viewWithTag:TIME_TAG];
	 label.text = [dateFormatter stringFromDate:[NSDate date]];
	 */
	// Set the image.
	UIButton *muteButton = (UIButton *)[cell.contentView viewWithTag:TIME_TAG];
	//[button setBackgroundImage:newNormalImage forState:UIControlState
	if(cellValue.muted == NO)
		[muteButton setImage:[UIImage imageNamed:@"Mute.png"] forState:UIControlStateNormal];
	else
		[muteButton setImage:[UIImage imageNamed:@"Unmute.png"] forState:UIControlStateNormal];
	
	[muteButton addTarget:self action:@selector(muteAction:) forControlEvents:UIControlEventTouchUpInside];
	[muteButton setTag:indexPath.row];
	//[muteButton setTitle:@"MUTE" forState:UIControlStateNormal];
	
	UIButton *removeButton = nil;
	
	removeButton = (UIButton *)[cell.contentView viewWithTag:IMAGE_TAG];
	
	if([[cell.contentView subviews] count] == 3)
		removeButton = (UIButton *)[[cell.contentView subviews] objectAtIndex:2];
	//
	//[button setBackgroundImage:newNormalImage forState:UIControlState
	if(cellValue.removed == NO)
		[removeButton setImage:[UIImage imageNamed:@"Remove.png"] forState:UIControlStateNormal];
	else
		[removeButton setImage:[UIImage imageNamed:@"Remove.png"] forState:UIControlStateNormal];
	
	[removeButton addTarget:self action:@selector(disconnectAction:) forControlEvents:UIControlEventTouchUpInside];
	[removeButton setTag:indexPath.row];
	
}    

- (void)muteAction:(id)sender
{ 
	UIButton* muteButton = (UIButton *)sender;
	NSLog(@"button %hu mute clicked!",[muteButton tag]);
	
	User *cellValue = [[SrnTeacherAppDelegate App].listOfAllUsers objectAtIndex:[muteButton tag]];
	if (cellValue.muted == YES) {
        cellValue.muted = NO;
        [sender setImage:[UIImage imageNamed:@"Mute.png"] forState:UIControlStateNormal];
    } else {
        cellValue.muted = YES;
        // set image on Hold button
        [sender setImage:[UIImage imageNamed:@"Unmute.png"] forState:UIControlStateNormal];
    }
	
}

- (void)disconnectAction:(id)sender
{ 
	UIButton* disconnectButton = (UIButton *)sender;
	NSLog(@"button %hu mute clicked!",[disconnectButton tag]);
	
	User *cellValue = [[SrnTeacherAppDelegate App].listOfAllUsers objectAtIndex:[disconnectButton tag]];
	
	cellValue.removed = YES;
	// set image on Hold button
	[sender setImage:[UIImage imageNamed:@"Romove.png"] forState:UIControlStateNormal];
	[cellValue.sock disconnect];
	
	[[SrnTeacherAppDelegate App].listOfAllUsers removeObjectAtIndex:[disconnectButton tag]];
	[self updateResult];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}



@end
